package com.ajk.server.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ajk.server.dao.DictionaryMapper;
import com.ajk.server.model.Dictionary;
import com.ajk.server.model.query.DictionaryQuery;
import com.ajk.server.service.DictionaryService;

/**
 * 全局字典表  <br>
 * t_dictionary <br>
 * v1.0 2016-05-01
 */
 @Service
 public class DictionaryServiceImpl implements DictionaryService {
	
	private static final Logger logger = LoggerFactory.getLogger(DictionaryServiceImpl.class);
	
	@Autowired
	private DictionaryMapper dictionaryMapper;
	
	/**
 	*根据ID删除 全局字典表
 	*/
 	@Override
 	public Integer deleteDictionaryById(Integer id){
 		return dictionaryMapper.deleteById(id);
 	}
 	
 	/**
 	*新增 全局字典表
 	*/
 	@Override
 	@Transactional
 	public Integer batchInsertDictionary(List<? extends Dictionary> dictionarys){
		Integer count = dictionaryMapper.batchInsert(dictionarys);
		return count;
 	}
 	/**
 	*新增 全局字典表
 	*/
 	@Override
 	@Transactional
 	public Integer insertDictionary(Dictionary domain){
		Integer id = dictionaryMapper.insert(domain);
		return id;
 	}

 	/**
 	*更新 全局字典表
 	*/
 	@Override
 	@Transactional
 	public Integer updateDictionary(Dictionary domain){
		Integer count = dictionaryMapper.update(domain);
		return count;
 	}
 	
	@Override
 	@Transactional
 	public Integer updateDictionaryByKey(Dictionary domain){
		Integer count = dictionaryMapper.updateByKey(domain);
		return count;
 	}
 	
 	/**
 	*根据ID查询 全局字典表
 	*/
 	public Dictionary findDictionaryById(Integer id){
 		Dictionary result = dictionaryMapper.findById(id);
 		if(result!=null){
 			result.dealWith();
 		}
 		return result;
 	}

	/**
 	*通过query 查询 全局字典表
 	*/
	public List<Dictionary> findDictionaryByQuery(DictionaryQuery query){
		query.dealWith();
		List<Dictionary> result = dictionaryMapper.findByQuery(query);
		if(CollectionUtils.isNotEmpty(result)) {
			for (Dictionary dictionary : result) {
				dictionary.dealWith();
			}
		}
		return result;
	}	
	
	/**
 	*通过query count总条数
 	*/
 	public Integer countDictionaryByQuery(DictionaryQuery query){
 		query.dealWith();
 		Integer result = dictionaryMapper.countByQuery(query);
 		return result;
 	}
 	
 	/**
 	* 通过ids查询
 	*/
 	public List<Dictionary> findDictionaryByIds(List<Integer> ids){
 		List<Dictionary> result = dictionaryMapper.findByIds(ids);
		if(CollectionUtils.isNotEmpty(result)) {
			for (Dictionary dictionary : result) {
				dictionary.dealWith();
			}
		}
		return result;
 	}
 	
 	@Override
 	public Map<String,String> findByKeys(List<String> keys){
 		Map<String,String> map = new HashMap<>();
 		List<Dictionary> result = dictionaryMapper.findByKeys(keys);
		if(CollectionUtils.isNotEmpty(result)) {
			for (Dictionary dictionary : result) {
				dictionary.dealWith();
				map.put(dictionary.getDictKey(), dictionary.getDictValue());
			}
		}
		return map;
 	}
 	
}	